package org.chen.wordwave.act;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import org.chen.wordwave.R;
import org.chen.wordwave.act.adapter.FavListAdapter;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.bean.WordListData;
import org.chen.wordwave.tools.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

public class Favorites extends BaseActivity {
    private Context context;
    private List<WordListData> favDataList;
    private Message message;
    private FavListAdapter adapter;
    private final int FAV_DATASET_DONE = 0x11;

    @SuppressLint("HandlerLeak")
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == FAV_DATASET_DONE) {

                adapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.favorites_layout_act;
    }

    @Override
    public void init() {
        context = Favorites.this;
        favDataList = new ArrayList<>();
        RecyclerView recycler_booklist = findViewById(R.id.fav_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        recycler_booklist.setLayoutManager(layoutManager);
        adapter = new FavListAdapter(context, favDataList);
        recycler_booklist.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        favDataList.clear();
        initData();
    }

    private void initData() {
        message = Message.obtain();
        new Thread(() -> {
            DBHelper dbHelper = new DBHelper(context, "Users.db", null, 1);
            SQLiteDatabase db = dbHelper.getReadableDatabase();
            String sql = "select * from wordlist where favorites=1;";
            Cursor cursor = db.rawQuery(sql, null);
            if (cursor.moveToFirst()) {
                do {
                    WordListData wordListData = new WordListData();
                    wordListData.setWordId(cursor.getInt(cursor.getColumnIndexOrThrow("word_id")));
                    wordListData.setDescription(cursor.getString(cursor.getColumnIndexOrThrow("desc_")));
                    wordListData.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
                    wordListData.setSymbol(cursor.getString(cursor.getColumnIndexOrThrow("symbol")));
                    wordListData.setMsg(cursor.getString(cursor.getColumnIndexOrThrow("msg")));
                    wordListData.setClassId(cursor.getString(cursor.getColumnIndexOrThrow("class_id")));
                    wordListData.setSound(cursor.getString(cursor.getColumnIndexOrThrow("sound")));
                    wordListData.setMemory(cursor.getInt(cursor.getColumnIndexOrThrow("memory")));
                    wordListData.setExampleSentence(cursor.getString(cursor.getColumnIndexOrThrow("example_sentence")));
                    wordListData.setFavorites(cursor.getInt(cursor.getColumnIndexOrThrow("favorites")));
                    favDataList.add(wordListData);
                } while (cursor.moveToNext());
            } else {
                WordListData wordListData = new WordListData();
                wordListData.setName(getString(R.string.non_favorites));
                favDataList.add(wordListData);
            }
            cursor.close();
            db.close();

            message.what = FAV_DATASET_DONE;
            handler.sendMessage(message);
        }).start();
    }
}
